<?
if($_SESSION['sid'] != 1){
    exit('are you ok?');
}
ob_start();
include("common/header.html");
include("common/left.html");
include("common/page.php");
include("class/SimpleExcel.class.php");
$out_html=ob_get_contents();
ob_end_clean();

$r=array();
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:10;
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$offset=($currentPage-1)*$pageSize;
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export")
{
	$currentPage = 1;
	$pageSize = 9999999;
}
$where1="where 1=1";
$dname=$_REQUEST['dname'];
if($dname&&is_numeric($dname)&&strlen($dname)==6||$dname=='admin'){
    $str="select id from pft_member where account='$dname'";
    $GLOBALS['le']->query($str);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $ids[]=$row['id'];
    }
    if(count($ids)!=0){
		$ids=implode(",",$ids);
		$where1.=" and id in ($ids)";
	}else{
		$where1.=" and 1<>1";
	}
}elseif($dname=mysql_real_escape_string(trim($_REQUEST['dname']))){
	$ids=array();
	$str="select id from pft_member where dname like '%".$dname."%'";
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$ids[]=$row['id'];
	}
	if(count($ids)!=0){
		$ids=implode(",",$ids);
		$where1.=" and id in ($ids)";
	}else{
		$where1.=" and 1<>1";
	}
}else{
    $where1.=" and id=$dname";
}

$where2="where 1=1";
if($beginDate=mysql_real_escape_string($_REQUEST['beginDate'])){
	$where2.=" and rectime>='$beginDate 00:00:00'";
}
if($endDate=mysql_real_escape_string($_REQUEST['endDate'])){
	$where2.=" and rectime<='$endDate 23:59:59'";
}
$where3="where 1=1";
if($_REQUEST['beginDate']!=""){
    $rectime=$_REQUEST['beginDate'];
    $where3.=" and rectime<'$rectime 00:00:00'";
}elseif($endDate=mysql_real_escape_string($_REQUEST['endDate'])){
    $rectime=$_REQUEST['endDate'];
    $where3.=" and rectime<'$rectime 00:00:00'";
}else{
    $rectime=date('Y-m-d',strtotime("-1 day"));
    $where3.=" and rectime<='$rectime 23:59:59'";
}


$where2.=" and ptype in (0,1,4,5)";
$where3.=" and ptype in (0,1,4,5)";//上期余额的条件
$where4=$where2." and daction=0";//收入的条件
$where5=$where2." and daction in(1,2)";//支出的条件
$dnames=array();
if($_REQUEST['beginDate']!=""&&$_REQUEST['endDate']!=""){
    $str="select id,dname,account from pft_member $where1 and dtype!=2 and dtype!=5 and dtype!=6 and status=0 limit $offset,$pageSize";
}else{
    $str="";
}
if($str!=""){
    $GLOBALS['le']->query($str);
    $dn=array();
    while($row=$GLOBALS['le']->fetch_assoc()){
        $dn[$row['id'].'_'.$row['account']]=$row['dname'];
        $dnames[$row['id']]=$row['dname'];
    }
    //将商户的account，id，dname存入ainfo数组
    $ainfo=$t=array();
    foreach($dn as $key=>$d){
        $k=explode('_',$key);
        $t['id']=$k[0];
        $t['dname']=$d;
        $t['account']=$k[1];
        $ainfo[]=$t;
        unset($t);
    }
}
$r1=$r2=array();
$r3=array();
if($ainfo){
	foreach($ainfo as $in=>$fo){
		if(is_numeric($fo['account'])&&strlen($fo['account'])==11) continue;
		$r3[]=$fo;
	}
}
//print_r($r3);
$ainfo=$r3;
if($_GET['no_test'])
    $test_id = include 'module/zax/admin_report/test_id.php';
if($ainfo){
    foreach($ainfo as $a=>$i){
        if($_GET['no_test']==1){if(in_array($i['id'],$test_id)) continue;}
        if($_GET['no_test']==2){if(!in_array($i['id'],$test_id)) continue;}
        $recharge=$sell=$profit=$msg=$code=$cancel=$other="";
		$cash=$buy=$poundage=$msg2=$code2=$cancel2=$other2="";
        if(is_numeric($i['account'])&&strlen($i['account'])==11) continue;
        //查当期余额
        $str1="select id,fid,dmoney,lmoney,aid,dtype,opid,ptype,'' as ali_account from pft_member_journal $where2 and fid={$i['id']} order by id desc,rectime desc limit 1";
        $GLOBALS['le']->query($str1);
        $row=$GLOBALS['le']->fetch_assoc();
        if($row){
            $row['account']=$i['account'];
            $row['member']=$i['dname'];
            $r1=$row;
        }else{
            $r1['account']=$i['account'];
            $r1['member']=$i['dname'];
            $r1['fid']=$i['id'];
        }
        //查上期余额
        $str3="select lmoney as lastmoney,rectime from pft_member_journal $where3 and fid={$i['id']} order by id desc,rectime desc limit 1";
        $GLOBALS['le']->query($str3);
        $GLOBALS['le']->fetch_assoc();
        $r2['lastmoney']=$GLOBALS['le']->f('lastmoney');
        $r2['rectime']=$GLOBALS['le']->f('rectime');
        if($r1['lmoney']==""){
            $r1['lmoney']=$r2['lastmoney'];
        }
		
		//当期收入
        $str4="select * from pft_member_journal $where4 and fid={$i['id']} order by id desc,rectime desc";
        $GLOBALS['le']->query($str4);
        while($ro=$GLOBALS['le']->fetch_assoc()){
			if($ro['dtype']==3){//充值
				$recharge+=$ro['dmoney'];
			}elseif($ro['dtype']==0){//销售产品
				$sell+=$ro['dmoney'];
			}elseif($ro['dtype']==5){//分润
				$profit+=$ro['dmoney'];
			}elseif($ro['dtype']==8){//代收短信/手续费
				$msg+=$ro['dmoney'];
			}elseif($ro['dtype']==7){//凭证收入
				$code+=$ro['dmoney'];
			}elseif($ro['dtype']==1){//退票
				$cancel+=$ro['dmoney'];
			}else{//其他
				$other+=$ro['dmoney'];
			}
            $inMoney+=$ro['dmoney'];
        }
        $r2['inmoney']=$inMoney;//当期收入
		$r2['recharge']=$recharge;
		$r2['sell']=$sell;
		$r2['profit']=$profit;
		$r2['msg']=$msg;
		$r2['code']=$code;
		$r2['cancel']=$cancel;
		$r2['other']=$other;
		//当期支出
        $str5="select * from pft_member_journal $where5 and fid={$i['id']} order by id desc,rectime desc";
        $GLOBALS['le']->query($str5);
        while($ros=$GLOBALS['le']->fetch_assoc()){
			if($ros['dtype']==6){//提现
				$cash+=$ros['dmoney'];
			}elseif($ros['dtype']==0){//购买产品
				$buy+=$ros['dmoney'];
			}elseif($ros['dtype']==9){//手续费
				$poundage+=$ros['dmoney'];
			}elseif($ros['dtype']==8){//短信
				$msg2+=$ros['dmoney'];
			}elseif($ros['dtype']==7){//凭证支出
				$code2+=$ros['dmoney'];
			}elseif($ros['dtype']==1){//退票
				$cancel2+=$ros['dmoney'];
			}else{//其他
				$other2+=$ros['dmoney'];
			}
            $outMoney+=$ros['dmoney'];
        }
        $r2['outmoney']=$outMoney;//当期支出
		$r2['cash']=$cash;
		$r2['buy']=$buy;
		$r2['poundage']=$poundage;
		$r2['msg2']=$msg2;
		$r2['code2']=$code2;
		$r2['cancel2']=$cancel2;
		$r2['other2']=$other2;
        $r[]=array_merge($r1,$r2);
        unset($inMoney);
        unset($outMoney);
        unset($r1);
        //print_r($r2);
        //echo "<br/>";
    }
}

//print_r($r[0]);
//exit;
if($_REQUEST['cs']){
    echo '<pre>';
    echo $str4;print_r($r);
    echo '</pre>';
    exit;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=="Export"){
	$filename=date('YmdHis').'交易记录统计';
	$Ecel[0]['account']='商户ID';
	$Ecel[0]['member']='商户名称';
    $Ecel[0]['rectime']='记录时间';
    $Ecel[0]['lastmoney']='上期余额';
    $Ecel[0]['recharge']='充值';
    $Ecel[0]['sell']='销售产品';
	$Ecel[0]['profit']='分润';
	$Ecel[0]['msg']='短信收入';
	$Ecel[0]['code']='凭证收入';
	$Ecel[0]['cancel']='退票收入';
	$Ecel[0]['other']='其他收入';
	$Ecel[0]['cash']='提现';
	$Ecel[0]['buy']='购买产品';
	$Ecel[0]['poundage']='手续费支出';
	$Ecel[0]['msg2']='短信支出';
	$Ecel[0]['code2']='凭证支出';
	$Ecel[0]['cancel2']='退票支出';
	$Ecel[0]['other2']='其他支出';
    $Ecel[0]['lmoney']='当期余额';
	$i=1;
    
	foreach($r as $lid=>$row){
        if(//都是空就不输出
        !$row['rectime'] && !$row['lastmoney'] && !$row['recharge'] && !$row['sell'] && !$row['profit'] && !$row['msg']
        && !$row['code'] && !$row['cancel'] && !$row['other'] && !$row['cash'] && !$row['buy'] && !$row['poundage']
         && !$row['msg2'] && !$row['code2'] && !$row['cancel2'] && !$row['other2'] && !$row['lmoney']
        )
            continue;
		$n++;       
        $Ecel[$n]['account']=$row['fid'];
        $Ecel[$n]['member']=$row['member'];
        $Ecel[$n]['rectime']=$row['rectime'];
        $Ecel[$n]['lastmoney']=$row['lastmoney']/100;
        $Ecel[$n]['recharge']=$row['recharge']/100;
        $Ecel[$n]['sell']=$row['sell']/100;
		$Ecel[$n]['profit']=$row['profit']/100;
		$Ecel[$n]['msg']=$row['msg']/100;
		$Ecel[$n]['code']=$row['code']/100;
        $Ecel[$n]['cancel']=$row['cancel']/100;
		$Ecel[$n]['other']=$row['other']/100;
		$Ecel[$n]['cash']=$row['cash']/100;
        $Ecel[$n]['buy']=$row['buy']/100;
		$Ecel[$n]['poundage']=$row['poundage']/100;
		$Ecel[$n]['msg2']=$row['msg2']/100;
		$Ecel[$n]['code2']=$row['code2']/100;
        $Ecel[$n]['cancel2']=$row['cancel2']/100;
		$Ecel[$n]['other2']=$row['other2']/100;
        $Ecel[$n]['lmoney']=$row['lmoney']/100;
	}
	$xs = new SimpleExcel('UTF-8', true,'trecordCountList');
    $xs->addArray($Ecel);
    $xs->generateXML($filename);
	exit;
}

if($str==""){
    $r[]="";
}
if($str!=""){
    $str="select id,account from pft_member $where1 and dtype!=2 and dtype!=5 and dtype!=6 and status=0";
    $GLOBALS['le']->query($str);
    while($row=$GLOBALS['le']->fetch_assoc()){
		$rr[]=$row;
	}
    $arr=array();
    if(count($rr))
        foreach($rr as $rw){
            if(is_numeric($rw['account'])&&strlen($rw['account'])==11) continue;
            $arr[]=$row;
        }
	$count=count($arr);
}     
$currentPage=$_REQUEST['currentPage']?abs($_REQUEST['currentPage']):1;
$pageSize=$_REQUEST['pageSize']?abs($_REQUEST['pageSize']):10;
$offset=($currentPage-1)*$pageSize;
echo $out_html;
include("tpl/trecordCount.html");
include("common/footer.html");
?>